<?php
namespace Mob \ Controller;

use Think \ Controller;
use Think \ Hook;

class TeacherController extends BaseController {

    private $UserInfo;

	public function _initialize() {
		parent :: _initialize();
		$this->_top_menu_list = array (
			'left' => array (
				array (
					'type' => 'home',
					'href' => U('Mob/Activity/activitymap'
				)
			),


		), 'center' => array (
			'title' => '知道'
		));
		if (is_login()) {





	  $this->_top_menu_list['right'][] = array (
					'type' => 'edit',
					'href' => U('Mob/Activity/creator'
				));

		} else {
			$this->_top_menu_list['right'][] = array (
				'type' => 'edit',
				'info' => '登录后才能操作！'
			);

		$this->redirect('Member/index');


		}
		//dump($this->_top_menu_list);exit;



		$this->UserInfo = query_user(array('nickname', 'avatar64', 'uid'), $v['uid']);
		$this->setMobTitle('知道');
		$this->assign('top_menu_list', $this->_top_menu_list);
	//print_r($this->_top_menu_list);
	}

	/**
	 * 主页面显示
	 */
	public function index() {


    $this->display();



	}
	public function myClassGroup()
	{
      $uid = get_uid();
     $myclass = D('Teacher')->get_TeacherClass($uid);


     foreach($myclass as &$v)
     {
     $v['personnel']= D('Student')->queryClassPersonnel($v['orgaId']);


     }
   // print_r($myclass);
    $this->assign('myclass',$myclass);
    $this->display();


	}
	public function dispaySingRecord()
	{

      $uid = get_uid();
     $myclass = D('Teacher')->get_TeacherClass($uid);
     $sign =D('Sign');

     foreach($myclass as &$v)
     {
     $v['class']=$sign->get_ClassSignDetails($v['orgaId']);

     }

     $this->assign('myclassgroup',$myclass);
     $this->display();
	}

    public function statisticsInfo()
    {

    $teacherId = get_uid();
    $teacher = D('Teacher');
    $teacherInfo = $teacher->query_Teacher($teacherId);
    $approvalRecord = D('Leave')->get_approvalhistory($teacherId,$teacherInfo['schooId']);  //展示请假审批结果

    $sign = D('Sign');
    $ContinuousNotSignTop =$sign->get_ContinuousNotSignTop($teacherId);       //统计累计连续未签到人员排行
    $notSignTop = $sign->get_NotSignTopFate($teacherId);
    $this->assign('notSignTop',$notSignTop);
    $this->assign('ContinuousNotSignTop',$ContinuousNotSignTop);
    $this->assign('approvalRecord',$approvalRecord);
    $this->display();

    }

    public function applyRegist()
    {
    $uid = get_uid();
     if(IS_POST)
     {
     $teacheName = I('post.teacherName',null,'string');
     $schooId = I('post.schooId','10183','int');
     $deptId = I('post.deptId',null,'int');
     $classGrade = I('post.classGrade',null,'int');
     $teacher = D('Teacher')->get_TeacherInfo($uid);
     if(empty($teacher))
     {
     $teacherOpenId = D('WxUser')->getUserInfo($uid)['OpenId'];
     $res = D('Teacher')->creator($uid,$teacherOpenId,$teacheName,$deptId,$classGrade,$schooId);
     if($res)
     {
     $data = array('status'=>1,'msg'=>'申请已提交，请耐心等待');
     }else
     {
     $data = array('status'=>0,'msg'=>'提交失败');
   // $data = array('status'=>0,'msg'=>M()->getLastSql());
     }
     }elseif($teacher['status']==0)
     {
     $data = array('status'=>0,'msg'=>'已提交，请耐心等待');
    //  $data = array('status'=>0,'msg'=>$teacher['status']);

     }elseif($teacher['status']==1)
     {
     $data = array('status'=>0,'msg'=>'已审核通过');
     }

      $this->ajaxReturn($data);

     }else
     {

     $teacher = D('Teacher')->get_TeacherInfo($uid);
     if(empty($teacher))
     {
     $regStat=3;
     }elseif($teacher['status']==0)
     {
     $regStat=2;

     }elseif($teacher['status']==1)
     {
     $regStat=1;
     }
     $this->assign('teacher',$teacher);
     $this->assign('regStat',$regStat);
     $this->display();

     }

    }


    public function classRecentSignStat()
   {
   	$classId = I('classId',110116,'int');
   	$schooId = I('schooId',10183,'int');
   	$uid = get_uid();
   	$ia_auth = D('Student')->verifyTeacherClassAuth($uid,$classId,$schooId);  //判断访问权限

   	if($ia_auth)   //如果有权限，则查询班组签到情况
   	{
   	$sign =D('Sign');
   $classSignRecord =  $sign->get_classRecentSignStatus($classId,$schooId);
   $className = D('Orga')->get_OrgaFullInfo($classId);

   	}
   	$ia_auth=$ia_auth==false?2:1;

    $this->assign('className',$className);
   	$this->assign('ia_auth',$ia_auth);
   	$this->assign('classSignRecord',$classSignRecord);
   	$this->display();

   }


   public function myClasses()
   {
    $teacherId = get_uid();
    $teacher = D('Teacher');
    $is_teacher=$teacher->query_Teacher($teacherId);
   if(!$is_teacher)
   {
    $this->redirect('Teacher/applyRegist',array('errmsg'=>'请先提供注册审核，谢谢！'));
   }
   $myClasses = $teacher->get_MyClassStudentRecord($teacherId,$is_teacher['schooId'],$is_teacher['level']);

   $this->assign('myClasses',$myClasses);
   $this->display();

   }
   public function classSignRecord()
   {
   $teacherId = get_uid();
    $teacher = D('Teacher');
    $is_teacher=$teacher->query_Teacher($teacherId);
   if(!$is_teacher)
   {
    $this->redirect('Teacher/applyRegist',array('errmsg'=>'请先提供注册审核，谢谢！'));
   }
   $myClasses = $teacher->get_MyClassSignRecord($teacherId,$is_teacher['schooId'],$is_teacher['level']);

   $this->assign('myClasses',$myClasses);
   $this->display();

   }


   public function classStatistics()
   {
   $teacherId = get_uid();
    $teacher = D('Teacher');
    $is_teacher=$teacher->is_teacher($teacherId);
   if(!$is_teacher)
   {
    $this->redirect('Teacher/applyRegist',array('errmsg'=>'请先提供注册审核，谢谢！'));
   }
   $teacherInfo = $teacher->query_Teacher($teacherId);
   $approvalRecord = D('Leave')->get_approvalhistory($teacherId,$teacherInfo['schooId']);  //展示请假审批结果
   $this->assign('approvalRecord',$approvalRecord);
   $this->display();

   }

   public function classStudentDetail()
   {
    $teacherId = get_uid();
    $teacher = D('Teacher');
    $classId = I('get.classId',null,'int');
    $schooId = I('get.schooId',10183,'int');
    $is_teacher=$teacher->is_teacher($teacherId);
    if(!$is_teacher)
     {
    $this->redirect('Teacher/applyRegist',array('errmsg'=>'请先提供注册审核，谢谢！'));
     }

   $student = D('Student')->queryClassPersonnel($classId,$schooId);
   $className = D('Orga')->get_OrgaFullInfo($classId);
   $this->assign('className',$className);
   $this->assign('student',$student);
   $this->display();
   }

   public function classLeaveApproval()
    {

     $teacherId = get_uid();
     if($teacherId==0)
     {
    $this->redirect('Member/index');

     }

    $teacher = D('Teacher')->query_Teacher($teacherId);
    $student = D('Student');
    if($teacher)
    {
    $leaveRecord = D('Leave')->authstrLeaveList($teacherId,$teacher['schooId']);

    $count =1;
    foreach($leaveRecord as &$v)
    {
    $v['sortId']= $count;
    $v['name']= $student->queryStudent($v['applyUid'])['name'];
    $count++;

    }
    unset($v);

   $this->assign('leaveRecord',$leaveRecord);
   $this->display();
    }
     }

   public function displaySignDetail()
   {

   $uid = I('get.StudentUid',get_uid(),'string');

   	$sign = D('Sign');
   	$student = D('Student')->queryStudent($uid);
   	$leave = D('Leave');
   $leaveRecord = $leave->personLeaveRecordByDate($uid,$student['schooId']);

   	if($student['is_sign']==1)   //判断类型为1的话，代表需要签到
   	{
   	$signRecord= $sign->get_personalSign1($uid,30);
   	}

   $this->assign('signRecord',$signRecord);
   $this->assign('leaveRecord',$leaveRecord);
   $this->assign('default',date('Y-m-d'));
   $this->display();

   }

   public function peopleManage()
   {
   	$manageUid = get_uid();
   $teacher = D('Teacher');
   $teacher_Info = $teacher->query_Teacher($manageUid);
   $applyList = $teacher->get_teacherDetail($manageUid,$teacher_Info['schooId']);
    $count =0;
    foreach($applyList as &$v)
    {
    $v['Dept']=D('Dept')->get_deptFullName($v['deptId']);
    $count++;
    $v['sortId']=$count;
    }
    unset($v);
    $this->assign('applyList',$applyList);
    $this->display();

   }
   public function approvalOperate()
   {
   	if(IS_POST)
   	{
   	$manageId = get_uid();
   	$teacherId = I('post.teacherUid',null,'int');
   	$schooId = I('post.schooId',10183,'int');
   	$verifyFlg = I('post.verifyFlg',null,'int');
   	$deptId = I('post.deptId',null,'int');
   	$is_auth = D('Dept')->is_authority($manageId,$deptId,$schooId);
   	if($is_auth)
   	{
   	$res = D('Teacher')->approvalVerify($teacherId,$verifyFlg,$manageId,$schooId);
   	if($res)
   	{
   	$data = array('status'=>1,'msg'=>'操作成功');
   	}else
   	{
   	$data = array('status'=>0,'msg'=>'操作失败');
   	}
   	}else
   	{
   	 $data = array('status'=>0,'msg'=>'您没有审核权限');
   	}
   $this->ajaxReturn($data);
   	}
   }
   public function removeOperate()
   {
    if(IS_POST)
    {
    $manageUid = get_uid();
    $teacherUid = I('post.teacherUid',null,'int');
    $deptId = I('post.deptId',null,'int');
    $schooId = I('post.schooId',10183,'int');
    $is_auth = D('Dept')->is_authority($manageUid,$deptId,$schooId);
    if($is_auth)
    {
    $res = D('Teacher')->removeOperate($teacherUid,$manageUid,$schooId);
    if($res)
    {
    $data = array('status'=>1,'msg'=>'操作成功');
    }else
    {
     $data = array('status'=>0,'msg'=>'操作失败');
    }
    }else
    {
    $data = array('status'=>0,'msg'=>'您没有操作权限');
    }
    $this->ajaxReturn($data);
    }
   }
  public function managerRegist()
  {
    $uid = get_uid();
     if(IS_POST)
     {
     $teacheName = I('post.teacherName',null,'string');
     $schooId = I('post.schooId','10183','int');
     $deptId = I('post.deptId',null,'int');
     $teacher = D('Teacher')->get_TeacherInfo($uid);
     if(empty($teacher))
     {
     $teacherOpenId = D('WxUser')->getUserInfo($uid)['OpenId'];
     $res = D('Teacher')->creator($uid,$teacherOpenId,$teacheName,$deptId,$classGrade,$schooId,2);
     if($res)
     {
     $data = array('status'=>1,'msg'=>'申请已提交，请耐心等待');
     }else
     {
    $data = array('status'=>0,'msg'=>'提交失败');
   // $data = array('status'=>0,'msg'=>M()->getLastSql());
     }
     }elseif($teacher['status']==0)
     {
     $data = array('status'=>0,'msg'=>'已提交，请耐心等待');
    //  $data = array('status'=>0,'msg'=>$teacher['status']);

     }elseif($teacher['status']==1)
     {
     $data = array('status'=>0,'msg'=>'已审核通过');
     }

      $this->ajaxReturn($data);

     }else
     {

     $teacher = D('Teacher')->get_TeacherInfo($uid);
     if(empty($teacher))
     {
     $regStat=3;
     }elseif($teacher['status']==0)
     {
     $regStat=2;

     }elseif($teacher['status']==1)
     {
     $regStat=1;
     }
     $this->assign('teacher',$teacher);
     $this->assign('regStat',$regStat);
     $this->display();
     }
     }

    public function directorApproval()
    {
    $usertype  = query_user('usertype');
    if(IS_POST)
    {
    if($usertype!=100)
    {
    $data = array('status'=>0,'msg'=>'权限不足');
    }else
    {
    $manageUid = get_uid();
    $teacherUid = I('post.teacherUid',null,'int');
    $schooId = I('post.schooId',10183,'int');
    $verifyFlg = I('post.verifyFlg',null,'int');
    $deptId = I('post.deptId',null,'int');
    $res = D('Teacher')->approvalVerify($teacherUid,$verifyFlg,$manageUid,$schooId);
    if($res)
    {
    D('Dept')->update_deptManageUid($deptId,$manageUid,$schooId);
     $data = array('status'=>1,'msg'=>'操作成功!');
    }else
    {
     $data = array('status'=>0,'msg'=>'操作失败!');
  //  $data = array('status'=>0,'msg'=>M()->getLastSql());
    }
    }
    $this->ajaxReturn($data);
    }else
    {
    $approvalList = D('Teacher')->get_waitAuditManage(10183);
    foreach($approvalList as &$v)
    {
    $v['dept']=D('Dept')->get_deptFullName($v['deptId']);
    }
    unset($v);
    $this->assign('directorList',$approvalList);
    $this->display();



    }
    }










}



